package io.github.wjehovah.yyn.utils;

import android.content.Context;
import android.util.Log;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import io.github.wjehovah.yyn.App;


public class JsonUtil {
    private static String newsJson = "";

    public static String getNewsJson() {
        return newsJson;
    }

    public static void setNewsJson(String newsJson) {
        JsonUtil.newsJson = newsJson;
    }

    private static String jsonCache = "";

    public static String getJsonCache() {
        return jsonCache;
    }

    public static void setJsonCache(String jsonString) {
        jsonCache = jsonString;
    }


    private static String readToString(InputStream inputStream) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        byte[] buffer = new byte[1024];
        int len;
        try {
            while ((len = inputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                byteArrayOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return byteArrayOutputStream.toString();
    }

    public static void setJsonDiskCache(String json, String fileName) {
        BufferedOutputStream bufferedOutputStream = null;

        try {
            File dir = App.getContext().getFilesDir();
            File target = new File(dir, fileName + ".json");
            target.deleteOnExit();
            bufferedOutputStream = new BufferedOutputStream(App.getContext()
                    .openFileOutput(fileName + ".json", Context.MODE_PRIVATE));
            bufferedOutputStream.write(json.getBytes("utf-8"));
            jsonCache = json;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bufferedOutputStream != null) {
                try {
                    bufferedOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static void setJsonDiskCache(String json) {
        setJsonDiskCache(json, "news");
    }


    public static String getJsonDiskCache(String fileName) {

        ByteArrayOutputStream byteArrayOutputStream = null;
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = App.getContext().openFileInput(fileName + ".json");
            byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while ((len = fileInputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, len);
            }
            return byteArrayOutputStream.toString();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (byteArrayOutputStream != null) {
                try {
                    byteArrayOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "";
    }


    public static String getJsonDiskCache() {
        Log.e("newCache", getJsonDiskCache("news"));
        return getJsonDiskCache("news");
    }


}
